package com.yw.swagger.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.yw.swagger.customNote.fyh;
import com.yw.swagger.model.SpKaoQin;
import com.yw.swagger.model.SysBpsGpmemlnk;
import com.yw.swagger.model.SysBpsUsers;
import com.yw.swagger.service.SpKaoQinService;
import com.yw.swagger.service.SysBpsUsersService;
import com.yw.swagger.service.WebMailService;
import com.yw.swagger.tool.AjaxResult;
import com.yw.swagger.tool.controller.BaseController;
import com.yw.swagger.tool.page.TableDataInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Controller
@Api(tags="考勤")
@RequestMapping("/sp_kaoqin")
public class SpKaoQinController extends BaseController {

    @Autowired
    WebMailService webMailService;

    @Autowired
    private SysBpsUsersService sysBpsUsersService;

    @Autowired
    SpKaoQinService spKaoQinService;

    @fyh
    @ApiOperation(value = "保存修改考勤",notes = "保存修改考勤")
    @ApiImplicitParams({
            @ApiImplicitParam(  name="author",value="权限码",required = true),
            @ApiImplicitParam(  name="date",value="日期1,日期2,日期3",required = true),
            @ApiImplicitParam(  name="account",value="操作者账号",required = true)
    })
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addKaoQin(SpKaoQin kaoQin, String date, String account, String dro) {
        List<String> datel =(List<String>) JSONUtils.parse(date);
        List<String> drol = (List<String>) JSONUtils.parse(dro);
//      List<Map<String, List<String>> dro;
        //查询有没有此人的考勤
        String fullname=spKaoQinService.selectKaoqinDetails(kaoQin.getFullname(),datel.get(0));
        System.err.println("fullName:"+fullname);
        int i=0;
        if(fullname==null){
            //添加考勤
            i=spKaoQinService.insertAddKaoQin(kaoQin,datel,drol);
        }else{
            //修改考勤
            i=spKaoQinService.updateKaoQin(kaoQin,datel,drol);
        }
        return AjaxResult.success(i);
    }

    @fyh
    @ApiOperation(value = "查询本部门考勤",notes = "查询本部门考勤")
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="send",value="账号",required = true),
            @ApiImplicitParam(name="ksDate",value="开始时间",required = true),
            @ApiImplicitParam(name="jsDate",value="结束时间",required = true)
    })
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @PostMapping("/listKaoQin")
    @ResponseBody
    public AjaxResult listKaoQin(String send,String ksDate,String jsDate) {
        //部门人员
        List<SysBpsGpmemlnk> pg = spKaoQinService.selectDeptPerson(send);
        List<String> person = new ArrayList<>();
        for (SysBpsGpmemlnk gpl : pg){
            person.add(gpl.getGpmemUserdspname());
        }
//      person.remove("超级管理员");
        System.err.println("person:"+person);
        //查询部门所有的人的考勤
        startPage();
        List<SpKaoQin> kaoQins=spKaoQinService.selectKaoqin(person,ksDate,jsDate);
        TableDataInfo td = getDataTable(kaoQins);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS,null);
        ar.put("rows",td.getRows());
        ar.put("total",td.getTotal());
        return ar;
    }

    @fyh
    @ApiOperation(value = "查询个人本周考勤",notes = "查询个人本周考勤")
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="send",value="账号",required = true),
    })
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @PostMapping("/personKaoQin")
    @ResponseBody
    public AjaxResult personKaoQin(String send) {
        //此账号用户名
        String userName=spKaoQinService.selectoaRoleName(send).getUSER_DspName();
        String []date=getTimeInterval(new Date()).split(",");
        List<SpKaoQin> kaoQins=spKaoQinService.selectPersonKaoqin(userName,date[0],date[1]);
        TableDataInfo td = getDataTable(kaoQins);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS,null);
        ar.put("rows",td.getRows());
        ar.put("total",td.getTotal());
        return ar;
    }

    @fyh
    @ApiOperation(value = "查询是否考勤权限",notes = "查询是否考勤权限")
    @ApiImplicitParams({
            @ApiImplicitParam(  name="author",value="权限码",required = true),
            @ApiImplicitParam(  name="send",value="账号",required = true)
    })
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @PostMapping("/kaoQinLimits")
    @ResponseBody
    public AjaxResult kaoQinLimits(String send) {
        SysBpsUsers user=spKaoQinService.selectoaRoleName(send);
        if(user==null){
            return error("账号异常");
        }
        //查询当前登录账号是否有考勤权限
        List<String>list=spKaoQinService.selectMoedl(user.getRole_ID().toString());
        List<String>limits=spKaoQinService.selectMoedlLimits(list);
        for(String str:limits){
            str.replace(" ","");
            if(str.equals("一周考勤")){
                return success("此用户有考勤权限");
            }
        }
        return error("此用户没有考勤权限");
    }


    @fyh
    @ApiOperation(value = "查询本部门人员",notes = "查询本部门人员")
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="send",value="账号",required = true)
    })
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @PostMapping("/listPerson")
    @ResponseBody
    public AjaxResult listPerson(String send){
        //部门人员
        startPage();
        List<SysBpsGpmemlnk>person= spKaoQinService.selectDeptPerson(send);
        for (SysBpsGpmemlnk p:person){
            String []date=getTimeInterval(new Date()).split(",");
            List<SpKaoQin> kaoQins=spKaoQinService.selectPersonKaoqin(p.getGpmemUserdspname(),date[0],date[1]);
            if(kaoQins.size() == 0){
                p.setCheckWork(false);
            }else{
                p.setCheckWork(true);
            }
        }

        TableDataInfo td = getDataTable(person);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS,null);
        ar.put("rows",td.getRows());
        ar.put("total",td.getTotal());
        return ar;
    }

    //本周日期
    public String getTimeInterval(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String imptimeBegin = sdf.format(cal.getTime());
        cal.add(Calendar.DATE, 6);
        String imptimeEnd = sdf.format(cal.getTime());
        return imptimeBegin + "," + imptimeEnd;
    }
}
